package com.stylefeng.guns.modular.system.task;

import java.util.Hashtable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.stylefeng.guns.core.common.constant.state.KpMethod;
import com.stylefeng.guns.core.util.HttpsClient;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.BrankMapper;
import com.stylefeng.guns.modular.system.dao.NiubilityBrankMapper;
import com.stylefeng.guns.modular.system.model.Brank;
import com.stylefeng.guns.modular.system.model.BusinessData;
import com.stylefeng.guns.modular.system.model.NiubilityBrank;
import com.stylefeng.guns.modular.system.model.YouziData;

@Service
public class YouziImportTask {

	@Autowired
	private BrankMapper brankMapper;
	
	@Autowired
	private NiubilityBrankMapper niubilityBrankMapper;
	
	public void execute() {
		for(int i=1;i<32;i++){
			Hashtable<String, String> a = new Hashtable<String, String>();
			a.put("a", KpMethod.B.getMethod());
			a.put("c", "UserBusiness");
			a.put("Day", "2018-01-"+i);	
			a.put("Token", "82a3a6f7cae7dbd0cb62cefedfe87b34");
			a.put("UserID", "387065");
			String result = HttpsClient.postSSL(ImportContants.getKpDataPathStr(), a,false);
			if(StringUtils.isNotEmpty(result)){
				List<YouziData> youzis = JSON.parseArray(JSON.parseObject(result).getString("list"),YouziData.class);
				if(CollectionUtils.isNotEmpty(youzis)){
					for(YouziData youziData : youzis){
						a = new Hashtable<String, String>();
						a.put("a", KpMethod.C.getMethod());
						a.put("c", "BusinessGroup");
						a.put("GID", youziData.getID()+"");	
						a.put("Token", "82a3a6f7cae7dbd0cb62cefedfe87b34");
						a.put("UserID", "387065");
						result = HttpsClient.postSSL(ImportContants.getKpDataPathStr(), a,false);
						if(StringUtils.isNotEmpty(result)){
							JSONObject object = JSON.parseObject(result);
							String dInfo = object.getString("Info");
							List<BusinessData> businessDatas = JSON.parseArray(object.getString("BusinessList"),BusinessData.class);
							if(CollectionUtils.isNotEmpty(businessDatas)){
								for(BusinessData businessData : businessDatas){
									String key = StringUtils.substringAfter(businessData.getName(), "证券");
									String na = StringUtils.substringBefore(businessData.getName(), "证券").concat("证券");
									List<Brank> branks = brankMapper.getBrankByName(key);
									if(CollectionUtils.isNotEmpty(branks)){
										for(Brank b : branks){
											if(StringUtils.contains(b.getgSalesName(), na)){
												NiubilityBrank niubilityBrank = new NiubilityBrank();
												niubilityBrank.setgSalesCode(Long.valueOf(b.getgSalesCode()));
												niubilityBrank = niubilityBrankMapper.selectOne(niubilityBrank);
												if(niubilityBrank == null){
													niubilityBrank = new NiubilityBrank();
													niubilityBrank.setgSalesCode(Long.valueOf(b.getgSalesCode()));
													niubilityBrank.setgSalesDetail(StringUtils.substringAfter(youziData.getName(), "：") + "--" + dInfo);
													niubilityBrankMapper.insert(niubilityBrank);
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
			}
		}
		
	}
}
